package com.example.demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AllRouters {


    @PostMapping("/hello2")
    public Mono<Void> selectMy(@RequestBody Map<String,Object> user, ServerHttpResponse response) {
//        System.out.println(user);
//        String pageNumber = request.getQueryParams().getFirst("pageNumber");
//        AuthUser authUser= LoginProcess.getCurrentUser(request.getCookies());
        Map<String, String> result = new HashMap<String, String>() {{
            put("aa", "1");
            put("bb", "21dd");
        }};
        return response.writeWith(Flux.create(sink -> {

            NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(new UnpooledByteBufAllocator(false));
            byte[] json = null;
            try {
                json = new ObjectMapper().writeValueAsBytes(result);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            DataBuffer dataBuffer = nettyDataBufferFactory.wrap(json);
            //DataBuffer dataBuffer= nettyDataBufferFactory.wrap("asdf".getBytes("utf8"));
            sink.next(dataBuffer);

            sink.complete();
        }));
    }
}